package com.annvcit.util;

import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;

 
/**
 * @author annvcit
 * @full_name Nguyen Van Chuc An
 * @date_created Dec 16, 2014
 * @email annvcit@gmail.com
 */
public class Loader {
	
	private static final String DEFAULT_PATH = "src/com/annvcit/resource/";
	public static final int LOAD_INT_MATRIX = 1;
	public static final int LOAD_DOUBLE_MATRIX = -1;
	
	public static AdjMatrix load(String fileName, int type) {
		AdjMatrix matrix = new AdjMatrix();
		try {
			BufferedReader reader = new BufferedReader(
					new FileReader(DEFAULT_PATH + fileName));
			
			String line;
			String v = reader.readLine();
			int nOfVerties = Integer.parseInt(v);
			
			int[][] intMtrx = new int[nOfVerties][nOfVerties];
			double[][] douMtrx = new double[nOfVerties][nOfVerties];
			StringTokenizer token;
			int row = 0;
			
			while ((line = reader.readLine()) != null) {
					token = new StringTokenizer(line);
					while (token.hasMoreTokens()) {
						
						if (type == LOAD_INT_MATRIX) {
							for (int i = 0; i < nOfVerties; i++) {
								intMtrx[row][i] = Integer.parseInt(token.nextToken());
							}
						}
						
						if (type == LOAD_DOUBLE_MATRIX) {
							for (int i = 0; i < nOfVerties; i++) {
								douMtrx[row][i] = Double.parseDouble(token.nextToken());
							}
						}
						row++;
					}
			} // end while
			matrix.setnOfVerties(nOfVerties);
			matrix.setIntMtrx(intMtrx);
			matrix.setDoubleMtrx(douMtrx);
			reader.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return matrix;
	}
}
